import 'dart:io';

import 'package:get/get.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/user/xindong_wode_user.dart';
import 'package:xindong/routes/tab_mine/xindong_wode_http.dart';
import 'package:xindong/utils/xindong_conversion_util.dart';
import 'package:xindong/utils/xindong_text_util.dart';

class XinDongProfileCtr extends GetxController {
  static XinDongProfileCtr get to => Get.find();

  final _mineHandle = XinDongWoDeHandle();
  late XinDongWoDeUser mineUser;

  File? avatarFile;

  @override
  void onInit() {
    super.onInit();
    // 深拷贝新对象
    final data = XinDongConversionUtil.modelToMap(XinDongUserController.to.userInfo);
    mineUser = XinDongWoDeUser.fromJson(data);
  }

  requestGetConfig() {
    _mineHandle.memberConfig();
    // momentHandle.requestListWithUserId(
    //   userId: userId,
    //   yemian: 0,
    //   yemianSize: 1,
    //   callback: (object) {
    //     if (object.code == XinDongServerStatus.success) {
    //       List list = object.result;
    //       if (list.isNotEmpty) {
    //         moments = list.map((e) => XinDongDongTaiClass.fromJson(e)).toList();
    //         update([XinDongKongJianController.updateIdLoadingMoment]);
    //       }
    //     }
    //   },
    // );
  }

  // 职业列表
  requestGetOccupation() async {
    if (XinDongAll.instance.occupationList.isEmpty) {
      final list = await _mineHandle.memberOccupation();
      XinDongAll.instance.occupationList = list;
    }
  }

  // 请求更新头像
  void requestUpdateAvatar(File? file) async {
    if (file == null) {
      return;
    }
    showLoading();
    final fileUrl = await XinDongNetworking.instance.uploadFile(filePath: file.path);
    if (TextUtil.isEmpty(fileUrl)) {
      hideLoading();
    } else {
      mDebugPrint('返回图片结果$fileUrl');
      var parameters = {"head": fileUrl};
      final object = await XinDongNetworking.instance.request(
        XinDongApi.userEditHead,
        method: XinDongHttpMethod.post,
        parameters: parameters,
      );
      if (object.code == XinDongServerStatus.success) {
        avatarFile = file;
        XinDongUserController.to.requestUserInfo();
      }
      hideLoading();
    }
    update();
  }

  //
  // regionLocation() async {
  //   XinDongGaoDeMap.to.startLocation();
  // }
  //
  // updateLocation() {
  //   if (!TextUtil.isEmpty(XinDongGaoDeMap.to.region)) this.tkUserInfoClass.cityName = XinDongGaoDeMap.to.region;
  //   if (XinDongGaoDeMap.to.currentCity != null) this.tkUserInfoClass.city = XinDongGaoDeMap.to.currentCity!.citysName;
  // }

  // 请求更新信息
  void requestUserUpdate() async {
    if (TextUtil.isEmpty(mineUser.name!.trim())) {
      showToast("请输入昵称");
      return;
    }
    if (mineUser.name!.trim().length > 12) {
      showToast("昵称1～12字");
      return;
    }
    // if (!tkUserInfoClass.wx!.availableWX) {
    //   showToast("微信号格式不符合");
    //   return;
    // }
    // {"name":"阿册啊","sex":"男","birthday":"2023-7-25","wxcount":"","qq":"",
    // "line_price":"0.00","qq_price":"","phone_price":"","city":"广州","offten_city":"",
    // "stature":"","emotion":"","education_background":"","monthly_salary":"",
    // "occupation":"","slogan":"","school_of_graduation":"","weight":"",
    // "through_membership":"","live_status":"","is_house_purchase":"","is_car_purchase":"",
    // "self_slogan":"","voice":"","dy_program":""}
    // Loading
    showLoading();
    var parameters = {
      "name": mineUser.name,
      "sex": mineUser.sex,
      "city": mineUser.city,
      "birthday": mineUser.birthday,
      "stature": mineUser.stature,
      "weight": mineUser.weight,
      "education_background": mineUser.education_background,
      "school_of_graduation": mineUser.school_of_graduation,
      "monthly_salary": mineUser.monthly_salary,
      "through_membership": mineUser.through_membership,
      "is_house_purchase": mineUser.is_house_purchase,
      "is_car_purchase": mineUser.is_car_purchase,
      "live_status": mineUser.live_status,
      "occupation": mineUser.occupation,
      "slogan": mineUser.slogan,
      "self_slogan": mineUser.self_slogan,
      "emotion": mineUser.emotion,
      "voice": mineUser.voice,
      "dy_program": mineUser.dy_program,
      // "wxcount": mineUser.wxcount ?? "",
      // "qq": mineUser.qq ?? "",
    };
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userEditInfo,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    // Loading
    hideLoading();
    if (object.code == XinDongServerStatus.success) {
      // 还需要请求一下用户信息接口
      // 用户数据
      XinDongUserController.to.requestUserInfo();
      Get.back();
    }
  }
}
